*' --------------
* sameness effects: macro-micro ----
*' ==============

clear all
* set up your working directory here
global home_dir ""

cd ${home_dir}

cap: program drop run_figure4
program run_figure4

args geo_type

use "./data/processed_notshared/cleaned_regtable_unemployment_`geo_type'_v2.dta", clear 

svyset [pw=weight] 	

if ("`vv'" == "v1"){
	keep if sample_v1 == 1	
}
if ("`vv'" == "v2") {
	keep if sample_v2 == 1	
}


local demographics_individual Female agegroup Race6 BornUSA 
local demographics_county RAT_Female RAT_AgeGrp4_2 RAT_AgeGrp4_3 RAT_AgeGrp4_4 RAT_Race6_2 RAT_Race6_3 RAT_Race6_4 RAT_Race6_5 RAT_Race6_6 RAT_BornUSA 
local contextual_control pop_density

egen n_miss = rowmiss(Suic St `geo_type' Year `demographics_individual' `demographics_county' `contextual_control')
recode n_miss (0=0) (1/max=1)

* for appendix table : compare emp/nonemp/notinlabor + overall
*sum Suic St `geo_type' Year `demographics_individual' `demographics_county' `contextual_control'

drop if n_miss > 0

*--------------
* Figure 3. interaction effects : Table S6, Model 1 to 4
*==============
local eq_demographics_individual Female i.agegroup i.Race6 BornUSA 
local eq_demographics_county RAT_Female RAT_AgeGrp4_2 RAT_AgeGrp4_3 RAT_AgeGrp4_4 RAT_Race6_2 RAT_Race6_3 RAT_Race6_4 RAT_Race6_5 RAT_Race6_6 RAT_BornUSA 
local eq_contextual_control pop_density
local eq_post_control RAT_EducGroup_1 RAT_EducGroup_2 RAT_EducGroup_3 RAT_EducGroup_4 RAT_EducGroup_5

local model_eq `eq_demographics_individual' `eq_demographics_county' `eq_contextual_control' 

estimates clear 
logit Suic i.St `model_eq' c.monthly_unemployment c.std_same_prop_empstat [pw=weight], cluster(`geo_type'), if empstat == 2
estimates store m_unemp_main

logit Suic i.St `model_eq' c.monthly_unemployment##c.std_same_prop_empstat [pw=weight], cluster(`geo_type'), if empstat == 2
estimates store m_unemp

logit Suic i.St `model_eq' c.monthly_unemployment##c.std_same_prop_empstat [pw=weight], cluster(`geo_type'), if empstat == 1
estimates store m_emp

logit Suic i.St `model_eq' c.monthly_unemployment##c.std_same_prop_empstat [pw=weight], cluster(`geo_type'), if empstat == 3
estimates store m_nlf

esttab m_unemp_main m_unemp m_emp m_nlf using "./output/table_logit_v2_`geo_type'_macrointeract.csv", csv replace label nogap se star (+ 0.1 * 0.05 ** 0.01) 

estimates restore m_unemp
margins, at(monthly_unemployment = (4 (0.5) 10)) post predict(pr)
matrix A = r(table)
matrix B = r(at)
mat2txt, matrix(A) saving("./output/margins/v1_margins_global_unemployed_sameness_`geo_type'.txt") replace
mat2txt, matrix(B) saving("./output/margins/v1_margins_at_global_unemployed_sameness_`geo_type'.txt") replace

estimates restore m_unemp
margins, dydx(std_same_prop_empstat) at(monthly_unemployment = (4 (0.5) 10)) post predict(pr)
matrix A = r(table)
matrix B = r(at)
mat2txt, matrix(A) saving("./output/margins/v1_mchange_global_unemployed_sameness_`geo_type'.txt") replace
mat2txt, matrix(B) saving("./output/margins/v1_mchange_at_global_unemployed_sameness_`geo_type'.txt") replace

estimates restore m_emp
margins, dydx(std_same_prop_empstat) at(monthly_unemployment = (4 (0.5) 10)) post predict(pr)
matrix A = r(table)
matrix B = r(at)
mat2txt, matrix(A) saving("./output/margins/v1_mchange_global_employed_sameness_`geo_type'.txt") replace
mat2txt, matrix(B) saving("./output/margins/v1_mchange_at_global_employed_sameness_`geo_type'.txt") replace

estimates restore m_nlf
margins, dydx(std_same_prop_empstat) at(monthly_unemployment = (4 (0.5) 10)) post predict(pr)
matrix A = r(table)
matrix B = r(at)
mat2txt, matrix(A) saving("./output/margins/v1_mchange_global_nlf_sameness_`geo_type'.txt") replace
mat2txt, matrix(B) saving("./output/margins/v1_mchange_at_global_nlf_sameness_`geo_type'.txt") replace

end

run_figure4 county
run_figure4 cz

